package com.java_foundation.thread_demo.test_10;

/**
 * @program: java_foundation
 * @description: 根据中断标志判断线程是否终止
 * @author: xiongbangwen <Email>xiongbangwen@163.com</Email>
 * @create: 2020-07-06 23:34
 **/
public class InterruptDemo {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                //如果当前线程被中断则退出循环
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread() + " hello");
                }
            }
        });
        //线程启动
        thread.start();

        //休眠0.02s
        Thread.sleep(20);

        //中断线程
        System.out.println("main方法所在线程中断thread的线程");
        thread.interrupt();

        //等待线程thread执行结束
        thread.join();
        System.out.println("方法结束");

    }

}
